%
% New version (06/01/2016) that saves a bit more information about each run
% In particular:
%   (1) which experiment the run comes from
%   (2) which track in that experiment the run comes from
%   (3) the elapsed time (0 = start of experiment) at the start of the run
% This might require some changes to the analysis code downstream, but
% will let us look more carefully at the behavior of individual larvae
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Something something something %
%
% Trying out some stuff with saving information about each turn, to be
% compiled elsewhere.
% 
% Should be run on data that has already been extracted by another script
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% This version (Misc2B.m for now) will focus on RUNs instead of REOs, which
% will obviate the need to use more than one text file, as we can keep
% track of the total time spent in runs of various orientations

% parameters...
setNumber = 1;
RUNstartframe = 1;
RUNendframe = 13133;
%RUNendframe = 4500;
%binSize = 1; % (in degrees, angles given as -180 to 180)
fileName = 'Ir25a^2 8-26C';


RUNall = eset.gatherField('run');
RUNallTracks = [RUNall.track];
RUNtrackstart = [RUNallTracks.startFrame];
RUNendind = [RUNall.endInd];
RUNinds = [RUNtrackstart] + int16([RUNendind]);
% Use this if we are only interested in a particular time range
RUNvalid = ([RUNinds]>=RUNstartframe & [RUNinds]<=RUNendframe)&[RUNendind]>0;
%
RUN = RUNall(RUNvalid);
RUNtracks = [RUN.track];
RUNexpts = [RUNtracks.expt];
RUNstartind = [RUN.startInd];

% basic information about each run
RUNtheta = [RUN.meanTheta];     % mean direction of travel during run
RUNtheta0 = [RUN.startTheta];   % initial direction of travel in the run
RUNlength = [RUN.pathLength];   % path length in pixels
RUNtime = [RUN.runTime];        % i.e. duration of the run in seconds
%
RUNtrackNum = [RUNtracks.trackNum];
for i=1:length(RUN)
   RUNexptNum(i)=find(eset.expt==RUNexpts(i));
   RUNtime0(i) = RUNtracks(i).dq.eti(RUNstartind(i));
   RUNsetNum(i)=setNumber;
end
% RUNsetNum = zeros(1,length(RUN));
%[RUNexptNum] = find(eset.expt==[RUNexpts]);
%RUNtime0 = [RUNtracks.dq.eti([RUNstartind])];

% make arrays (full of zeros)
RUNxpos = zeros(1,length(RUN));
RUNreoYN = boolean(zeros(1,length(RUN)));
RUNreoHS = zeros(1,length(RUN));
RUNreotheta1 = zeros(1,length(RUN));
RUNreotheta2 = zeros(1,length(RUN));
RUNreoHS1 = zeros(1,length(RUN));

% loop through all the runs and get what we need
for i=1:length(RUN)
    
    % find the end position of each run
    runEndInd = RUN(i).endInd;
    if(runEndInd>RUN(i).track.npts)
       runEndInd = RUN(i).track.npts; 
    end
    loc = RUN(i).track.pt(runEndInd).loc;
    RUNxpos(i) = loc(1);
    
    % find info. about the turn at the end of the run, if there is one
    if(~isempty(RUN(i).nextReorientation))
       if(~isempty(RUN(i).nextReorientation.headSwing))
           %
           RUNreoYN(i)=true;
           %
           RUNreoHS(i)=RUN(i).nextReorientation.numHS;
           %
           RUNreoHS1(i) = RUN(i).nextReorientation.headSwing(1).maxTheta;
           %
           RUNreotheta1(i) = RUN(i).nextReorientation.prevDir;
           %
           RUNreotheta2(i) = RUN(i).nextReorientation.nextDir;        
           %
       end
    end
     
end

% Save the results in a text file...
fileID = fopen(fileName,'w');
fprintf(fileID,'%14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\t %14s\r\n','set','expt','track','time0','reoYN','runQ','runL','runT','runX','reo#HS','reoQ1','reoQ2','reoHS1','runQ0');
A = [RUNsetNum;RUNexptNum;RUNtrackNum;RUNtime0;RUNreoYN;RUNtheta;RUNlength;RUNtime;RUNxpos;RUNreoHS;RUNreotheta1;RUNreotheta2;RUNreoHS1;RUNtheta0];
fprintf(fileID,'%14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\t %14f\r\n',A);
fclose(fileID);




